Utils = {};

Utils.log = function(...)
    if type(...) == "table" then
        table.print(...);
    else
        print(...);
    end
end

Utils.isNil = function(go)
    return go == nil or CS.Util.IsNull(go);
end

Utils.addClick = function(btn, fun)
    btn.button.onClick:AddListener(function()
        SoundMgr:playSound("click");
        fun();
    end);
end

Utils.setRtfPos = function(rtf, x, y)
    rtf.anchoredPosition = Vector2(x, y);
end

Utils.setTfPos = function(tf, x, y)
    tf.localPosition = Vector3(x, y, 0);
end

Utils.setVisible = function(go, b)
    go.gameObject:SetActive(b);
end

Utils.setGray = function(img, b)
    CS.Util.SetUIGray(img, b);
end

Utils.isGray = function(img)
    return img.material ~= nil;
end

Utils.setProgress = function(slider, value)
    if value <= 0 and slider.value > 0 then
        slider.fillRect.gameObject:SetActive(false);
    elseif value > 0 and slider.value <= 0 then
        slider.fillRect.gameObject:SetActive(true);
    end
    slider.value = value;
end

Utils.split = function(str, value)
    return CS.StringUtil.Split(str, value);
end

Utils.replace = function(str, oldValue, newValue)
    return CS.StringUtil.Replace(str, oldValue, newValue);
end

Utils.toLower = function(str)
    return CS.StringUtil.ToLower(str);
end

Utils.contains = function(str, value)
    return CS.StringUtil.Contains(str, value);
end

Utils.fileExists = function(filesPath)
    return CS.Util.FileExists(filesPath);
end

Utils.pathGetExtension = function(path)
    return CS.Util.PathGetExtension(path);
end

Utils.pathGetFileName = function(path)
    return CS.Util.PathGetFileName(path);
end

Utils.readAllLines = function(filesPath)
    return CS.Util.ReadAllLines(filesPath);
end

Utils.htmlColor = function(str, color)
    if not color then
        color = "#ffffff";
    end
    return string.format("<color=%s>%s</color>", color, str);
end

Utils.htmlHref = function(str, text, link)
    if text and link then
        text.onHrefClick = (function(link)
            if link == "nil" then
                return;
            end
            Utils.log("link",link);
        end)
    end
    return string.format("<a href=%s>%s</a>", link, str);
end

Utils.htmlImage = function(name, y, x)
    if not x then
        x = 0;
    end
    if not y then
        y = 0;
    end
    return string.format("<quad name=%s,x=%s,y=%s/>", name, x, y);
end

Utils.addRedotImage = function(go, state)
    local redot = go.transform:Find("redot");
    if not redot then
        redot = GameObject():AddComponent(typeof(Image));
        redot.gameObject.name = "redot";
        redot.transform:SetParent(go.transform, false);
        redot.sprite = ResMgr:getAtlasprite("TinyGame/TinyGameAtlas", "redot");
        redot:SetNativeSize();
        redot.raycastTarget = false;
        local rtf = redot.transform:GetComponent(typeof(RectTransform));
        rtf.anchorMax = Vector2(1, 1);
        rtf.anchorMin = Vector2(1, 1);
        rtf.anchoredPosition = Vector2(0, 0);
    end
    redot.gameObject:SetActive(state);
end
